package edu.wong.controller;

import edu.wong.entities.Pay;
import edu.wong.entities.PayDTO;
import edu.wong.resp.ResultData;
import edu.wong.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "支付微服务",description = "支付CRUD")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法，json串做参数")
    public ResultData<String> addAccount(@RequestBody Pay account) {
        int add = payService.add(account);
        return ResultData.success("成功插入记录" + add);
    }

    @DeleteMapping(value = "/pay/del/{id}")
    public ResultData<Integer> deleteAccount(@PathVariable("id") Integer id) {
        return ResultData.success(payService.delete(id));
    }

    @PutMapping(value = "/pay/update")
    public ResultData<String> updateAccount(@RequestBody PayDTO payDTO) {
        Pay account = new Pay();
        BeanUtils.copyProperties(payDTO,account);
        int i = payService.update(account);
        return ResultData.success("成功修改记录" + i);
    }

    @GetMapping(value = "/pay/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id")Integer id){

        return ResultData.success(payService.getById(id));

    }

    @GetMapping(value = "/pay/getAll")
    @Operation(summary = "获得全部",description = "获得数据库中的全部内容")
    public ResultData<List<Pay>> getAll(){
        List<Pay>list= payService.getAll();
        return ResultData.success(list);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}")String atguiguInfo){
        return "atguiguInfo: "+atguiguInfo+"\t"+"port: "+port;
    }
}
